package com.example.libraryapp.network.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.libraryapp.R;
import com.example.libraryapp.network.net.RetrofitException;
import com.example.libraryapp.widget.CnToolbar;

/**
 * @author 伍凯铭
 * @since 2020/2/29
 */
public abstract class BaseActivity<V extends IBaseView,P extends BasePresenter<V>> extends AppCompatActivity implements IBaseView{

    protected P mPresenter;

    protected View mContent;

    protected View mNetError;

    protected View mLoadError;

    protected View mEmpty;

    protected View mLoading;

    protected CnToolbar mToolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        ViewGroup container = findViewById(R.id.content);
        mToolbar = findViewById(R.id.toolbar);
        if(isShowToolBar()) {
            mToolbar.setVisibility(View.VISIBLE);
        }else {
            mToolbar.setVisibility(View.GONE);
        }
        initToolBar(mToolbar);

        mContent = LayoutInflater.from(this).inflate(getLayoutId(),container,false);
        container.addView(mContent);
        initView(mContent);
        //presenter
        mPresenter = initPresenter();
        if(mPresenter != null) {
            mPresenter.attachView((V) this);
        }
    }

    protected boolean isShowToolBar() {
        return true;
    }

    protected void initToolBar(CnToolbar toolbar) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mPresenter != null) {
            mPresenter.detachView();
        }
    }

    protected abstract void initView(View view);

    protected abstract int getLayoutId();

    protected abstract P initPresenter();

    protected void reload() {

    }

    @Override
    public void showLoading() {
        mContent.setVisibility(View.GONE);
        if(mLoading == null) {
            ViewStub stub = findViewById(R.id.viewStub_loading);
            mLoading = stub.inflate();
        }
        if(mLoadError != null) {
            mLoadError.setVisibility(View.GONE);
        }
        if(mEmpty != null) {
            mEmpty.setVisibility(View.GONE);
        }
        if(mNetError != null) {
            mNetError.setVisibility(View.GONE);
        }
        mLoading.setVisibility(View.VISIBLE);
    }

    @Override
    public void showNoNetwork(RetrofitException.ResponseThrowable error) {
        mContent.setVisibility(View.GONE);
        if(mNetError == null) {
            ViewStub stub = findViewById(R.id.viewStub_net_error);
            mNetError = stub.inflate();
            mNetError.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    reload();
                }
            });
        }
        if(mLoadError != null) {
            mLoadError.setVisibility(View.GONE);
        }
        if(mEmpty != null) {
            mEmpty.setVisibility(View.GONE);
        }
        if(mLoading != null) {
            mLoading.setVisibility(View.GONE);
        }
        mNetError.setVisibility(View.VISIBLE);
    }

    @Override
    public void showError(RetrofitException.ResponseThrowable error) {
        mContent.setVisibility(View.GONE);
        if(mLoadError == null) {
            ViewStub stub = findViewById(R.id.viewStub_load_error);
            mLoadError = stub.inflate();
        }
        if(mNetError != null) {
            mNetError.setVisibility(View.GONE);
        }
        if(mEmpty != null) {
            mEmpty.setVisibility(View.GONE);
        }
        if(mLoading != null) {
            mLoading.setVisibility(View.GONE);
        }
        mLoadError.setVisibility(View.VISIBLE);
    }

    @Override
    public void showEmpty() {
        mContent.setVisibility(View.GONE);
        if(mEmpty == null) {
            ViewStub stub = findViewById(R.id.viewStub_empty);
            mEmpty = stub.inflate();
        }
        if(mNetError != null) {
            mNetError.setVisibility(View.GONE);
        }
        if(mLoadError != null) {
            mLoadError.setVisibility(View.GONE);
        }
        if(mLoading != null) {
            mLoading.setVisibility(View.GONE);
        }
        mEmpty.setVisibility(View.VISIBLE);
    }

    @Override
    public void showContent() {
        if(mLoadError != null) {
            mLoadError.setVisibility(View.GONE);
        }
        if(mEmpty != null) {
            mEmpty.setVisibility(View.GONE);
        }
        if(mLoading != null) {
            mLoading.setVisibility(View.GONE);
        }
        if(mNetError != null) {
            mNetError.setVisibility(View.GONE);
        }
        mContent.setVisibility(View.VISIBLE);
    }
}
